package com.talent.study.hello;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: hszhou
 * @Date: 2025/6/5 16:02
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class ReadMain {

    public static void main(String[] args) throws Exception {

        // 读取JSON文件并转换为Demo对象列表
//
//
//        List<Demo> juyuan = readJsonFile("D:\\talent-parent\\talent-study\\src\\main\\java\\com\\talent\\study\\hello\\juyuan.json");
//
//        List<Demo> oulutong = readJsonFile("D:\\talent-parent\\talent-study\\src\\main\\java\\com\\talent\\study\\hello\\oulutong.json");
//
//        List<Demo> yingfeite = readJsonFile("D:\\talent-parent\\talent-study\\src\\main\\java\\com\\talent\\study\\hello\\yingfeite.json");
//
//        d3C(bolandeList, juyuan, oulutong, yingfeite,"3C.xlsx");
//        d3C(
//                bolandeList.stream().filter(e->e.getTeststandard().contains("42296-2022")).collect(Collectors.toList()),
//                juyuan.stream().filter(e->e.getTeststandard().contains("42296-2022")).collect(Collectors.toList()),
//                oulutong.stream().filter(e->e.getTeststandard().contains("42296-2022")).collect(Collectors.toList()),
//                yingfeite.stream().filter(e->e.getTeststandard().contains("42296-2022")).collect(Collectors.toList()),
//                "3C-42296.xlsx");

        dianchizu();
    }

    private static void dianchizu() throws IOException {
        List<Demo> bolandeList = readJsonFile("D:\\talent-parent\\talent-study\\src\\main\\java\\com\\talent\\study\\hello\\dianchi.json");

        Map<String, List<Demo>> data = bolandeList.stream().collect(Collectors.groupingBy(Demo::getManuname));

        ExcelWriter excelWriter  = EasyExcel.write("电池组充电器.xlsx", Demo.class).build();
        // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
        Set<Map.Entry<String, List<Demo>>> entries = data.entrySet();


        int i=0;
        for (Map.Entry<String, List<Demo>> entry : entries) {
            List<Demo> value = entry.getValue();
            String key = entry.getKey();
            log.info("【{}】",key);
            WriteSheet writeSheet = EasyExcel.writerSheet(i,  key).build();
            excelWriter.write(value, writeSheet);
            i++;
        }

        // 千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

    private static void d3C(List<Demo> bolandeList ,  List<Demo> juyuan ,      List<Demo> oulutong ,List<Demo> yingfeite,String fileName) {

            // 打印转换后的对象列表

            HashMap<String,List<Demo>> data = new HashMap<>();
            data.put("博兰得",bolandeList);
            data.put("聚源电子",juyuan);
            data.put("深圳欧陆通",oulutong);
            data.put("杭州英飞特",yingfeite);

            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可

            ExcelWriter excelWriter  = EasyExcel.write(fileName, Demo.class).build();
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
            Set<Map.Entry<String, List<Demo>>> entries = data.entrySet();


            int i=0;
            for (Map.Entry<String, List<Demo>> entry : entries) {
                List<Demo> value = entry.getValue();
                String key = entry.getKey();
                WriteSheet writeSheet = EasyExcel.writerSheet(i,  key).build();
                excelWriter.write(value, writeSheet);
                i++;
            }

            // 千万别忘记finish 会帮忙关闭流
            excelWriter.finish();


    }

    public static List<Demo> readJsonFile(String filePath) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        File jsonFile = new File(filePath);

        // 读取JSON文件并解析为根对象
        Root root = mapper.readValue(jsonFile, Root.class);

        // 从根对象中提取Demo对象列表
        return root.getRows();
    }

    // 根对象类，用于映射JSON文件的整体结构
    private static class Root {
        private int total;
        private List<Demo> rows;
        private String message;

        // Getters and Setters
        public int getTotal() { return total; }
        public void setTotal(int total) { this.total = total; }

        public List<Demo> getRows() { return rows; }
        public void setRows(List<Demo> rows) { this.rows = rows; }

        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
    }
}
